package com.company;


import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import java.io.*;

import com.sun.image.codec.jpeg.*;

import java.io.*;

public class Main {


    public static class WnetWScreenRecorder extends Thread {

        private Dimension screenSize;
        private Rectangle rectangle;
        private Robot robot;
        private long i = 0;
        private JPEGImageEncoder encoder;

        public WnetWScreenRecorder() {
            screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            rectangle = new Rectangle(screenSize);//可以指定捕获屏幕区域
            try {
                robot = new Robot();
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println(e);
            }
        }

        public static void main(String[] args) {
            new WnetWScreenRecorder().start();
        }

        public void run() {
            FileOutputStream fos = null;
            while (true) {
                try {
                    BufferedImage image = robot.createScreenCapture(rectangle);//捕获制定屏幕矩形区域
                    fos = new FileOutputStream(System.getProperty("user.dir") + "/recode/" + i + ".jpg");
                    JPEGCodec.createJPEGEncoder(fos).encode(image);//图像编码成JPEG
                    fos.close();
                    i = i + 1;
                    Thread.sleep(40);//每秒25帧
                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println(e);
                    try {
                        if (fos != null) fos.close();
                    } catch (Exception e1) {
                    }
                }
            }
        }
    }


//播放屏幕录像


    public static class WnetWScreenRecordPlayer extends JFrame {
        BorderLayout borderLayout1 = new BorderLayout();
        Dimension screenSize;

        public WnetWScreenRecordPlayer() {
            super();
            screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            this.setSize(screenSize);
            Screen p = new Screen();
            Container c = this.getContentPane();
            c.setLayout(borderLayout1);
            c.add(p, "Center");
            new Thread(p).start();
            this.show();
        }

        public static void main(String[] args) {
            new WnetWScreenRecordPlayer();
        }

    }

    static class Screen extends JPanel implements Runnable {
        private BorderLayout borderLayout1 = new BorderLayout();
        private Image cimage;

        public void run() {
            int i = 0;
            while (true) {
                try {
                    cimage = loadImage(i + ".jpg");
                    i = i + 1;
                    repaint();
                    Thread.sleep(40);//与录像时每秒帧数一致

                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println(e);
                }
            }
        }

        public Image loadImage(String name) {
            Toolkit tk = Toolkit.getDefaultToolkit();
            Image image = null;
            while(image == null){
                image = tk.getImage(System.getProperty("user.dir") + "/recodes/"  + name);
            }
            MediaTracker mt = new MediaTracker(this);
            mt.addImage(image, 0);
            try {
                mt.waitForID(0);
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println(e);
            }
            return image;
        }

        public Screen() {
            super();
            this.setLayout(null);
        }

        public void paint(Graphics g) {
            super.paint(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.drawImage(cimage, 0, 0, null);
        }
    }


}
